﻿using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using WLFramework.Security;
using WLFramework.Data;
using WLFramework.Data.Linq;
using WLFramework.DataAccess;
using GenericHtml;

namespace SampleSite.admin
{
    public partial class Default : KeyValuePage
    {
        private string html;

        protected void Page_Load(object sender, EventArgs e)
        {
            using (PermissionContext db = new PermissionContext())
            {
                var user = Session["current_user"] as sys_User;


                var cmd = DataAccessor.CreateInstance<PermissionCommand>(db);
                var query = cmd.GetApplicationByUserID(this.LoginUser.U_ID);
                
                /*
                var roles = from p in db.sys_UserRoles
                            where p.R_State == 1 && p.U_ID == this.LoginUser.U_ID
                            select p.R_ID;

                var query = (from p in db.sys_RoleApplication
                            where p.A_State == 1 &&
                                  p.sysRoleApplicationsysApplications.A_State == 1 &&
                                  roles.Contains(p.R_ID)
                            select new 
                            {
                                p.sysRoleApplicationsysApplications.A_CName,
                                p.sysRoleApplicationsysApplications.A_ID
                            }).Distinct();
                */

                HtmlDocument ul = new HtmlDocument();
                ul.LoadHtml("<ul></ul>");
                foreach (var app in query)
                {
                    
                    var li = ul.CreateElement("li");
                    var a = ul.CreateElement("a");
                    li.AppendChild(a);

                    a.Attributes.Add(
                        "href",
                        "/admin/application.aspx?appid=" +
                            AES.AESEncrypt(app.A_ID.ToString()));
                    a.AppendChild(ul.CreateTextNode(app.A_CName));

                    ul.DocumentNode.FirstChild.AppendChild(li);
                }

                this.html = ul.DocumentNode.WriteTo();
            }
        }

        public string GetApplicationList()
        {
            return html;
        }
    }
}